package com.work.game.home.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.volley.Request;
import com.anythink.nativead.api.ATNativeAdView;
import com.gyf.immersionbar.ImmersionBar;
import com.work.game.R;
import com.work.game.ad.MediationFeedAd;
import com.work.game.ad.MediationTablePlaqueAd;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdCate;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdHome;
import com.work.game.ad.kaku.nativie.TaKuNativeAdCate;
import com.work.game.ad.kaku.nativie.TaKuNativeAdHome;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.adapter.CateAdapter;
import com.work.game.home.ui.adapter.GoodsAdapter;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.inter.OnClickCateListener;
import com.work.game.home.ui.model.Category;
import com.work.game.home.ui.model.Good;
import com.work.game.home.ui.model.vo.BaseVo;
import com.work.game.volley.BaseFragment;
import com.work.game.volley.state.HUCState;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;

public class CategoryFragment extends BaseFragment implements OnClickCateListener {
    private View view;
    private GridView gvGoods;
    private ListView lvCategory;

    private GoodsAdapter goodsAdapter;
    private CateAdapter cateAdapter;
    private boolean isVisibleToUser = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (ObjectUtil.isNotNull(view)) {
            return view;
        }
        view = inflater.inflate(R.layout.view_page_category, container, false);
        super.onCreateView(inflater, container, savedInstanceState);
        if (isVisibleToUser) {
            initAd();
        }
        initCate();
        initGoods();
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            initAd();
        }
    }

    @Override
    public void initView() {
        super.initView();
        gvGoods = view.findViewById(R.id.goods);
        lvCategory = view.findViewById(R.id.category);
        ImmersionBar.with(this).titleBar(view.findViewById(R.id.title)).init();
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (!(response instanceof List)) {
                    return;
                }
                cateAdapter = new CateAdapter(this.getContext(), this, (List<Category>) response);
                lvCategory.setAdapter(cateAdapter);
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                if (!(response instanceof List)) {
                    return;
                }
                goodsAdapter = new GoodsAdapter(this.getContext(), (List<Good>) response);
                gvGoods.setAdapter(goodsAdapter);
                break;
            default:
                break;
        }
    }

    private void initGoods() {
        hucRequestInterfase.executeStr(this, Good.class, HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.URL_PRO_GOODS, hucParameInterfase.getRequestParameter(new BaseVo()), false);
    }

    private void initCate() {
        hucRequestInterfase.executeStr(this, Category.class, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_PRO_CATEGORY, hucParameInterfase.getRequestParameter(new BaseVo()), false);
    }


    @Override
    public void onClickCateListener(String id) {
        BaseVo baseVo = new BaseVo();
        baseVo.setId(id);
        hucRequestInterfase.executeStr(this, Good.class, HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.URL_PRO_GOODS, hucParameInterfase.getRequestParameter(baseVo), false);
    }

    private void initAd() {
        //Banner广告
        if (ObjectUtil.isNull(view)) {
            return;
        }
        ATNativeAdView viewGroup = view.findViewById(R.id.fl_content);
        String key = getSharedPreferences(RedisEnum.TA_KU.getKey());
        if (RedisEnum.TA_KU.getValue().equals(key)) {
            TaKuNativeAdCate taKuNativeAdCate = TaKuNativeAdCate.getInstance();
            taKuNativeAdCate.setmATNativeAdView(viewGroup);
            taKuNativeAdCate.setToken(getSharedPreferences("deviceId"));
            taKuNativeAdCate.showAd();

            TaKuInterstitialAdCate taKuInterstitialAdCate = TaKuInterstitialAdCate.getInstance();
            taKuInterstitialAdCate.setToken(getSharedPreferences("deviceId"));
            taKuInterstitialAdCate.showAd(this.getActivity());
        } else {
            //信息流
            MediationFeedAd mediationFeedAd = MediationFeedAd.getInstance();
            mediationFeedAd.setFlFeed(viewGroup);
            mediationFeedAd.setActivity(this.getActivity());
            mediationFeedAd.showCacheFeedAd();
            //插屏广告
            MediationTablePlaqueAd mediationTablePlaqueAd = MediationTablePlaqueAd.getInstance();
            mediationTablePlaqueAd.showCacheInterstitialFullAd(this.getActivity(), null);
        }
    }
}
